<?php
/**
 * Created by IntelliJ IDEA.
 * User: admin
 * Date: 5/25 0025
 * Time: 11:41
 */

namespace app\api\model;

/**
 * Class Wxpay 改进版微信类
 * @package app\api\model
 */
class Wxpay
{
    public function __construct()
    {
        //        自动注册,构造函数中可能出现参数 TODO
        defined("APPID") or define("APPID", 'wx28e4cb89c38339a9');
        defined("MCHID") or define("MCHID","1354563802");
        defined("KEY") or define("KEY","j34893h89955e3jj29jd90j320kk3j29");
        defined("APPSECRET") or define("APPSECRET",'a2051fc02dbc9484243d98f56d9f9149');
        spl_autoload_register(function($classname){
            include_once EXTEND_PATH."cwx".DIRECTORY_SEPARATOR."lib".DIRECTORY_SEPARATOR.$classname.".php";
        });
    }

    /**
     * 生成一个预订单并
     * @param $out_trade_no
     * @param $total_fee
     * @return bool|mixed
     */
    public function getqrurl($out_trade_no,$total_fee){

        $unifiedOrder = new \WxPayUnifiedOrder();
        $unifiedOrder->SetBody("充值订单"); //商品或支付单简要描述
        $unifiedOrder->SetOut_trade_no($out_trade_no);
        $unifiedOrder->SetTotal_fee($total_fee);
        $unifiedOrder->SetTrade_type("NATIVE");
        $unifiedOrder->SetNotify_url(url("Notify/wxnotify",null,null,$_SERVER["HTTP_HOST"]));//支付回调URL
        $unifiedOrder->SetProduct_id(133);//NATIVE时，必须传product_id 商户自定义
        $result = \WxPayApi::unifiedOrder($unifiedOrder);
        if(is_array($result) and isset($result["code_url"])){
            return $result["code_url"];
        }else{
            return false;
        }
    }


}